package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.entity.Flight;
import com.guotd.ticketSystem.mapper.FlightMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getFlights")
public class GetFlightsServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = SessionUtil.getSqlSession();
        FlightMapper flightMapper = session.getMapper(FlightMapper.class);
        List<Flight> flights = flightMapper.getFlights();
        System.out.println(flights);
        if (!flights.isEmpty()) {
            request.setAttribute("flights", flights);
            request.getSession().setAttribute("flight_msg", "查询成功！");
            request.getRequestDispatcher("flight_list.jsp").forward(request, response);
        } else {
            request.getSession().setAttribute("flight_msg", "抱歉，未查询到任何航班！");
            request.getRequestDispatcher("flight_list.jsp").forward(request, response);
        }
    }
}